package com.villaday.idf.base;

import android.content.Context;

import androidx.recyclerview.widget.RecyclerView;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * RecyclerView的单布局适配器封装
 * 可以实现Gridview和瀑布流的效果
 */
public abstract class BaseRecyclerViewAdapter<T> extends RecyclerView.Adapter<BaseRecyclerViewAdapter.RecyclerViewHolder> {

    protected Context context;
    protected List<T> datas;
    private int resid;

    private OnItemClickListener onItemClickListener;
    private View.OnLongClickListener onItemLongClickListener;

    public BaseRecyclerViewAdapter(Context context, int resid) {
        this.context = context;
        this.resid = resid;
        datas = new ArrayList<>();
    }

    public BaseRecyclerViewAdapter(Context context, List<T> datas, int resid) {
        this.context = context;
        this.datas = datas;
        this.resid = resid;
    }

    public void setDatas(List<T> datas) {
        this.datas = datas;
        this.notifyDataSetChanged();
    }

    public void addDatas(List<T> datas) {
        this.datas.addAll(datas);
        this.notifyDataSetChanged();
    }

    public List<T> getDatas() {
        return datas;
    }

    public void deleteItem(int position) {
        this.datas.remove(position);
        this.notifyDataSetChanged();
    }

    /**
     * item点击监听器
     *
     * @param onItemClickListener
     */

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener {
        void onItemClick(View v, int position);
    }

    /**
     * item长按监听器
     *
     * @param onItemLongClickListener
     */
    public void setOnItemLongClickListener(View.OnLongClickListener onItemLongClickListener) {
        this.onItemLongClickListener = onItemLongClickListener;
    }

    @Override
    public int getItemCount() {
        if (datas == null) {
            return 0;
        }
        return datas.size();
    }

    @Override
    public RecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(resid, parent, false);
        return new RecyclerViewHolder(view);
    }

    /**
     * 配置数据
     *
     * @param holder   类型必须是BaseRecyclerViewAdapter.RecyclerViewHolder
     * @param position
     */
    @Override
    public abstract void onBindViewHolder(BaseRecyclerViewAdapter.RecyclerViewHolder holder, int position);

    /**
     * 创建ViewHolder
     */
    protected class RecyclerViewHolder extends RecyclerView.ViewHolder {
        /**
         * 该Map用于缓存布局中的控件
         */
        Map<Integer, View> cacheMap = new HashMap();
        View layoutView;

        /**
         * 该构造参数 需要传递一个布局对象过来
         *
         * @param itemView
         */
        public RecyclerViewHolder(View itemView) {
            super(itemView);
            if (onItemClickListener != null) {
                itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int position = RecyclerViewHolder.this.getLayoutPosition();
                        onItemClickListener.onItemClick(v, position);
                    }
                });


            }
            if (onItemLongClickListener != null) {
                itemView.setOnLongClickListener(onItemLongClickListener);
            }
            this.layoutView = itemView;
        }

        /**
         * 通过控件ID 找到该控件
         *
         * @param id
         * @return
         */
        public View getView(int id) {
            if (cacheMap.containsKey(id)) {
                return cacheMap.get(id);
            } else {
                View view = layoutView.findViewById(id);
                cacheMap.put(id, view);
                return view;
            }
        }

        /**
         * 绑定TextView
         *
         * @param id
         * @param text
         * @return
         */
        public RecyclerViewHolder bindTextView(int id, String text) {
            TextView tv = (TextView) getView(id);
            tv.setText(text);
            return this;
        }

        /**
         * 绑定TextView 含html标签
         *
         * @param id
         * @param text
         * @return
         */
        public RecyclerViewHolder bindTextViewWithHtml(int id, String text) {
            TextView tv = (TextView) getView(id);
            tv.setText(Html.fromHtml(text));
            return this;
        }

        /**
         * 绑定ImageView
         *
         * @param id
         * @param imgId
         * @return
         */
        public RecyclerViewHolder bindImageView(int id, int imgId) {
            ImageView iv = (ImageView) getView(id);
            iv.setImageResource(imgId);
            return this;
        }


        /**
         * 设置进度条进度
         *
         * @param id
         * @param num
         * @return
         */
        public RecyclerViewHolder bindProgressBar(int id, int num) {
            ProgressBar progressBar = (ProgressBar) getView(id);
            progressBar.setProgress(num);
            return this;
        }

        /**
         * 设置item宽度
         *
         * @param width
         */
        public RecyclerViewHolder setLayoutViewWidth(int id, int width) {
            View view = getView(id);
            view.setMinimumWidth(width);
            return this;
        }

        /**
         * 设置item高度
         *
         * @param height
         */
        public RecyclerViewHolder setLayoutViewHeight(int height) {
            layoutView.setMinimumHeight(height);
            return this;
        }
    }
}
